package org.jeecg.modules.guard.service.impl;

import org.jeecg.modules.guard.entity.AgGuard;
import org.jeecg.modules.guard.entity.AgGuardDetail;
import org.jeecg.modules.guard.mapper.AgGuardDetailMapper;
import org.jeecg.modules.guard.mapper.AgGuardMapper;
import org.jeecg.modules.guard.service.IAgGuardService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 门禁主表
 * @Author: jeecg-boot
 * @Date:   2020-05-25
 * @Version: V1.0
 */
@Service
public class AgGuardServiceImpl extends ServiceImpl<AgGuardMapper, AgGuard> implements IAgGuardService {

	@Autowired
	private AgGuardMapper agGuardMapper;
	@Autowired
	private AgGuardDetailMapper agGuardDetailMapper;
	
	@Override
	@Transactional
	public void saveMain(AgGuard agGuard, List<AgGuardDetail> agGuardDetailList) {
		agGuardMapper.insert(agGuard);
		if(agGuardDetailList!=null && agGuardDetailList.size()>0) {
			for(AgGuardDetail entity:agGuardDetailList) {
				//外键设置
				entity.setGuard(agGuard.getId());
				agGuardDetailMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(AgGuard agGuard,List<AgGuardDetail> agGuardDetailList) {
		agGuardMapper.updateById(agGuard);
		
		//1.先删除子表数据
		agGuardDetailMapper.deleteByMainId(agGuard.getId());
		
		//2.子表数据重新插入
		if(agGuardDetailList!=null && agGuardDetailList.size()>0) {
			for(AgGuardDetail entity:agGuardDetailList) {
				//外键设置
				entity.setGuard(agGuard.getId());
				agGuardDetailMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		agGuardDetailMapper.deleteByMainId(id);
		agGuardMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			agGuardDetailMapper.deleteByMainId(id.toString());
			agGuardMapper.deleteById(id);
		}
	}
	
}
